<?php

App::uses('AppModel', 'Model');

/**
 * Project Model
 *
 * @property User $User
 * @property ProjectType $ProjectType
 * @property PropertyType $PropertyType
 * @property Amphur $Amphur
 * @property Image $Image
 * @property ProjectDetail $ProjectDetail
 */
class Project extends AppModel {

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'code' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Please enter project code.',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'user_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'project_type_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Please select project type.',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'property_type_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Please select property type.',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'ProjectType' => array(
            'className' => 'ProjectType',
            'foreignKey' => 'project_type_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'PropertyType' => array(
            'className' => 'PropertyType',
            'foreignKey' => 'property_type_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Amphur' => array(
            'className' => 'Amphur',
            'foreignKey' => 'amphur_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'Image' => array(
            'className' => 'Image',
            'foreignKey' => 'project_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'ProjectDetail' => array(
            'className' => 'ProjectDetail',
            'foreignKey' => 'project_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    public function listAllDetail($project_type_id = NULL, $property_id = NULL, $propertyTypeId = NULL, $limit = NULL) {
        $this->Behaviors->load('Containable');
        $condis = array(
            'Project.status' => 'yes',
        );
        if($project_type_id != NULL && $project_type_id !=0){
            $condis = $condis+array('Project.project_type_id' => $project_type_id);
        }
        if($property_id != NULL && $property_id !=0){
            $condis = $condis+array('PropertyType.property_id' => $property_id);
        }
        if($propertyTypeId != NULL && $propertyTypeId !=0){
            $condis = $condis+array('Project.property_type_id' => $propertyTypeId);
        }
        //debug($condis);
        $data = $this->find('all', array(
            'fields' => array(
                'Project.id', 'Project.code', 'Project.default_pic', 'Project.project_type_id', 'Project.property_type_id'
            ),
            'contain' => array(
                'ProjectDetail' => array(
                    'fields' => array(
                        'ProjectDetail.language', 'ProjectDetail.title','ProjectDetail.price'
                    ),
                    'conditions' => array(
                        'ProjectDetail.language' => CakeSession::read('Config.language')
                    ),
                ),
                'PropertyType' => array(
                    'fields' => array(
                        'PropertyType.name_tha', 'PropertyType.name_eng',
                    ),
                    'conditions' => array(
                    ),
                    'Property' => array(
                        'fields' => array(
                            'Property.id', 'Property.name_tha', 'Property.name_eng'
                        ),
                    ),
                ),
                'Amphur' => array(
                    'fields' => array(
                        'Amphur.id', 'Amphur.name_tha', 'Amphur.name_eng'
                    ),
                    'Province' => array(
                        'fields' => array(
                            'Province.id', 'Province.name_tha', 'Province.name_eng'
                        ),
                    ),
                ),
            ),
            'conditions' => $condis,
            'order' => array('created desc'),
            'limit' => $limit,
            'recursive' => -1
        ));
        return $data;
    }

    public function listAllAdmin($propertyId = NULL, $projectTypeId = NULL) {
        $this->Behaviors->load('Containable');
        $condi = array();

        if ($propertyId != NULL) {
            $condi = $condi+array(
                'PropertyType.property_id' => $propertyId//Resi
                
            );
        }
        if($projectTypeId != NULL){
            $condi = $condi+array(
                'Project.project_type_id' => $projectTypeId//Resi
                
            );
        }       

        $data = $this->find('all', array(
            'fields'=>array(
                'Project.status','Project.code','Project.created','PropertyType.name_eng'
            ),
            'contain' => array(
                'ProjectDetail' => array(
                    'fields'=>array(
                        'ProjectDetail.title'
                    ),
                    'conditions' => array(
                        'ProjectDetail.language' => 'eng'
                    ),
                ),
                'User' => array(
                    'fields'=>array('User.username')
                ),
               
            ),
            'joins' => array(
                array(
                    'table' => 'property_types',
                    'alias' => 'PropertyType',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Project.property_type_id = PropertyType.id',
                    )
                ),
            ),
            'conditions' => $condi,
            'recursive' => -1
        ));
        return $data;
    }

    public function listAllGuest($propertyId,$projectTypeId) {
        $this->Behaviors->load('Containable');
        $condi = array(
            'Project.status' => 'yes',
        );

        if($propertyId != NULL){
            $condi = $condi+array(
                'PropertyType.property_id' => $propertyId//Resi
            );
        }
        if($projectTypeId != NULL){
            $condi = $condi+array(
                'Project.project_type_id' => $projectTypeId, //New
            );
        }

        $data = $this->find('all', array(
            'contain' => array(
                'ProjectDetail' => array(
                    'conditions' => array(
                        'ProjectDetail.language' => 'eng'
                    ),
                ),
            ),
            'joins' => array(
                array(
                    'table' => 'property_types',
                    'alias' => 'PropertyType',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Project.property_type_id = PropertyType.id',
                    )
                ),
            ),
            'conditions' => $condi,
            'recursive' => -1
        ));
        return $data;
    }

    public function getProject($lang, $projectId) {
        $this->Behaviors->load('Containable');
        if ($lang == THAI_LANG) {
            $lang = THAI_LANG;
        } else {
            $lang = ENG_LANG;
        }
        $data = $this->Find('first', array(
            'contain' => array(
                'ProjectDetail' => array(
                    'conditions' => array(
                        'ProjectDetail.language' => $lang
                    )
                ),
                'PropertyType' => array(
                ),
                'Amphur' => array(
                    'Province' => array(
                    ),
                ),
            ),
            'conditions' => array(
                'Project.id' => $projectId
            ),
            'recursive' => -1
        ));
        return $data;
    }

    public function getNewResiden() {
        $this->Behaviors->load('Containable');
        $lang = CakeSession::read('Config.language');
        if ($lang == NULL) {
            $lang = ENG_LANG;
        }
        $data = $this->Find('all', array(
            'contain' => array(
                'ProjectDetail' => array(
                    'conditions' => array(
                        'ProjectDetail.language' => $lang
                    ),
                ),
            ),
            'joins' => array(
                array(
                    'table' => 'property_types',
                    'alias' => 'PropertyType',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Project.property_type_id = PropertyType.id',
                    )
                ),
            ),
            'conditions' => array(
                'Project.status' => 'yes',
                'project_type_id' => 1, //New
                'PropertyType.property_id' => 1//Resi
            ),
            'limit' => LIMIT_FOR_MENU,
            'recursive' => -1
        ));
        return $data;
    }

    public function getNewCommer() {
        $lang = CakeSession::read('Config.language');
        $this->Behaviors->load('Containable');
        if ($lang == NULL) {
            $lang = ENG_LANG;
        }
        $data = $this->Find('all', array(
            'contain' => array(
                'ProjectDetail' => array(
                    'conditions' => array(
                        'ProjectDetail.language' => $lang
                    ),
                ),
            ),
            'joins' => array(
                array(
                    'table' => 'property_types',
                    'alias' => 'PropertyType',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Project.property_type_id = PropertyType.id',
                    )
                ),
            ),
            'conditions' => array(
                'Project.status' => 'yes',
                'project_type_id' => 1, //New
                'PropertyType.property_id' => 2//Commer
            ),
            'limit' => LIMIT_FOR_MENU,
            'recursive' => -1
        ));
        return $data;
    }

    public function checkDuplicate($data) {
        $count = $this->find('count', array('conditions' => array(
                'Project.code' => $data['code']
        )));

        if ($count == 0) {
            return true;
        }
        return false;
    }

    public function listByType($type) {
        $data = $this->Find('all', array(
            'conditions' => array(
                'Project.property_type_id' => $type
            )
        ));
        return $data;
    }

    public function listAll($properId) {
//        if ($db == NULL || $db == '') {
//            $db = 'eng';
//        }
//        $this->useDbConfig = $db;
        if ($properId == 0 || $properId == '') {
            $data = $this->Find('all', array(
                'conditions' => array(
                ),
                'contain' => array(
                    'ProjectDetail' => array(
                        'conditions' => array(
                        ),
                    ),
                ),
            ));
        } else {
            $data = $this->Find('all', array(
                'conditions' => array(
                    'Project.property_type_id' => $properId
                ),
                'order' => array('Project.status asc', 'Project.title asc')
            ));
        }
        return $data;
    }

    public function listActive($properId, $language = NULL) {
        if ($properId == 0 || $properId == '') {
            $data = $this->Find('all', array(
                'conditions' => array(
                    'Project.status' => 'Y',
                    'Project.language' => $language
                ),
                'order' => array('Project.status asc', 'Project.title asc')
                    //'recursive'=>2
            ));
        } else {
            $data = $this->Find('all', array(
                'conditions' => array(
                    'Project.property_type_id' => $properId,
                    'Project.status' => 'Y',
                    'Project.language' => $language
                ),
                'order' => array('Project.status asc', 'Project.title asc')
            ));
        }
        return $data;
    }

    public function search($type_for, $property_type_id, $location, $preferences, $bedroom) {

        if ($property_type_id == '' || $property_type_id == 0) {
            $property_type_id = '';
        }
        $conditions = array();
        if ($property_type_id == '' && $type_for == '') {
            $conditions = array(
                'Project.location' => $location,
                'OR' => array(
                    'Project.bedroom' => $bedroom,
                    'ProjectPreference.preference_id' => $preferences
                )
            );
        } else if ($property_type_id == '' && $type_for != '') {
            $conditions = array(
                'Project.location' => $location,
                'Project.type_for' => $type_for,
                'OR' => array(
                    'Project.bedroom' => $bedroom,
                    'ProjectPreference.preference_id' => $preferences
                )
            );
        } else if ($property_type_id != '' && $type_for == '') {
            $conditions = array(
                'Project.location' => $location,
                'Project.property_type_id' => $property_type_id,
                'OR' => array(
                    'Project.bedroom' => $bedroom,
                    'ProjectPreference.preference_id' => $preferences
                )
            );
        } else {
            $conditions = array(
                'Project.property_type_id' => $property_type_id,
                'Project.location' => $location,
                'Project.type_for' => $type_for,
                'OR' => array(
                    'Project.bedroom' => $bedroom,
                    'ProjectPreference.preference_id' => $preferences
                )
            );
        }

        $data = $this->Find('all', array(
            'joins' => array(
                array('table' => 'project_preferences',
                    'alias' => 'ProjectPreference',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Project.code = ProjectPreference.project_code',
                    )
                )
            ),
            'conditions' => $conditions
        ));

        return $data;
    }

}
